package com.example.secondhandshop.ui.adapter;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.secondhandshop.R;
import com.example.secondhandshop.bean.Allchatuser;
import com.example.secondhandshop.bean.Chatitem;
import com.example.secondhandshop.ui.activity.MessageDetails;

import java.util.Date;
import java.util.List;

public class AllchatuserAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private List<Allchatuser> allchatusers;
    public AllchatuserAdapter(List<Allchatuser> allchatusers) {
        this.allchatusers = allchatusers; // 聊天信息列表
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view;
        view = inflater.inflate(R.layout.message_list_item, parent, false);
        return new ChatUserViewHolder(view);
    }
    public void updateChatUsers(List<Allchatuser> allchatusers) {
        this.allchatusers = allchatusers;
        notifyDataSetChanged(); // 更新适配器中的数据显示
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        Allchatuser allchatuser = allchatusers.get(position);
        System.out.println("allchatuser is "+allchatuser);// 获取当前消息
        AllchatuserAdapter.ChatUserViewHolder chatViewHolder=(AllchatuserAdapter.ChatUserViewHolder) holder;
        chatViewHolder.send_user.setText(allchatuser.getUsername());
        chatViewHolder.unread_message.setText(Integer.toString(allchatuser.getUnReadNum()));
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(view.getContext(), MessageDetails.class);
                intent.putExtra("sellerId", (int) allchatuser.getFromUserId()); // 卖家id
                intent.putExtra("sellername", allchatuser.getUsername()); // 卖家用户名
                System.out.println(allchatuser.getUnReadNum());
                intent.putExtra("unread",allchatuser.getUnReadNum());//未读条数
                view.getContext().startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {
        return allchatusers.size();
    }
    static class ChatUserViewHolder extends RecyclerView.ViewHolder {
        TextView send_user;
        TextView send_time;
        TextView send_massage;
        TextView unread_message;
        ImageView imageView;
        public ChatUserViewHolder(View itemView) {
            super(itemView);
            send_user = itemView.findViewById(R.id.send_user);
            send_time = itemView.findViewById(R.id.send_time);//先默认为发送方
            send_massage = itemView.findViewById(R.id.send_massage);
            unread_message=itemView.findViewById(R.id.unread_message);
            imageView = itemView.findViewById(R.id.send_userimage); // 头像
        }
    }
}
